package com.vessel.decoration.mvp.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jess.arms.di.component.AppComponent;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportFragment;
import com.vessel.decoration.mvp.ui.fragment.main.HomeTabFragment;
import com.vessel.decoration.mvp.ui.fragment.main.MeTabFragment;
import com.vessel.decoration.mvp.ui.fragment.main.BbsTabFragment;
import com.vessel.decoration.mvp.ui.fragment.main.ShopCartTabFragment;
import com.vessel.decoration.mvp.ui.widget.TabEntity;

import java.util.ArrayList;

import butterknife.BindString;
import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;

/**
 * @author vesselzhang
 * @date 2017/11/25
 */

public class NavHomeFragment extends MySupportFragment {

    private ISupportFragment[] mFragments = new ISupportFragment[4];

    @BindView(R.id.bottom_bar)
    CommonTabLayout mBottomBar;
    @BindString(R.string.main_home)
    String homeTitle;
    @BindString(R.string.main_bbs)
    String bbsTitle;
    @BindString(R.string.main_shopping_cart)
    String shoppingCartTitle;
    @BindString(R.string.main_mine)
    String mineTitle;

    public static NavHomeFragment newInstance() {
        return new NavHomeFragment();
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_nav, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        initFragmentation();
        initBottomBar();
    }


    @Override
    public void setData(Object data) {
    }

    private void initFragmentation() {
        ISupportFragment homeFragment = findChildFragment(HomeTabFragment.class);
        if (homeFragment == null) {
            mFragments[0] = HomeTabFragment.newInstance();
            mFragments[1] = BbsTabFragment.newInstance();
            mFragments[2] = ShopCartTabFragment.newInstance();
            mFragments[3] = MeTabFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_content, 0, mFragments);
        } else {
            mFragments[0] = findChildFragment(HomeTabFragment.class);
            mFragments[1] = findChildFragment(BbsTabFragment.class);
            mFragments[2] = findChildFragment(ShopCartTabFragment.class);
            mFragments[3] = findChildFragment(MeTabFragment.class);
        }
    }

    private void initBottomBar() {
        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        mTabEntities.add(new TabEntity(homeTitle, R.drawable.bt_shouye_h, R.drawable.bt_shouye_n));
        mTabEntities.add(new TabEntity(bbsTitle, R.drawable.bt_xiaoxi_h, R.drawable.bt_xiaoxi_n));
        mTabEntities.add(new TabEntity(shoppingCartTitle, R.drawable.bt_gouwu_h, R.drawable.bt_gouwu_n));
        mTabEntities.add(new TabEntity(mineTitle, R.drawable.bt_wode_h, R.drawable.bt_wode_n));
        mBottomBar.setTabData(mTabEntities);
        mBottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
//                Timber.e("position = " + position);
                showHideFragment(mFragments[position]);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }
}
